package com.share.homestay.pojo;

import com.alibaba.fastjson.annotation.JSONField;

import com.share.homestay.entity.SysUser;
import com.share.homestay.entity.TjPermissions;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

@Data
@NoArgsConstructor
public class LoginUser  implements UserDetails{
    // 用户信息
    private SysUser sysUser;
    // 权限信息
    private List<String> permissions;
    private List<TjPermissions> menus;
    @JSONField(serialize = false)
    private List<SimpleGrantedAuthority> authorities;
    public LoginUser(SysUser sysUser, List<String> permissions, List<TjPermissions> menus){
        this.sysUser = sysUser;
        this.permissions = permissions;
        this.menus = menus;
    }

    public LoginUser(SysUser sysUser, List<String> perms) {
        this.sysUser = sysUser;
        this.permissions = perms;
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        // 将权限信息封装成 SimpleGrantedAuthority
        if (authorities != null) {
            return authorities;
        }
        authorities = this.permissions.stream().map(SimpleGrantedAuthority::new).collect(Collectors.toList());
        return authorities;
    }

    @Override
    public String getPassword() {
        return this.sysUser.getPassword();
    }

    @Override
    public String getUsername() {
        return this.sysUser.getUserName();
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }
    @Override
    public boolean isEnabled() {
        return true;
    }
}